<?php
declare (strict_types=1);

namespace app\api\controller\order;

use app\admin\model\cart\Cart;
use app\admin\model\store\GoodsSku;
use app\api\controller\Base;
use app\common\service\user\UserRetailService;
use app\validate\OrderPayCheck;
use app\common\service\OrderService;
use app\Request;
use app\validate\OrderCreateCheck;
use think\facade\Cache;

class Order extends Base
{
    /**
     * 显示资源列表
     * @return \think\Response
     */

    public function getPaginate()
    {
        return $this->result([], 200, '成功', 'json');
    }

    /**
     * Author:William
     * 订单中心
     * 什么都不传是全部的订单, 0：待付款、2：待收货、3：待评价、9售后(交易成功）
     */
    public function orderCenter(Request $request)
    {
        $statusCode = $request->get('orders_status_code');
        $uid = $this->request->uid;
        $res = OrderService::orderStatusInfo($statusCode,$uid);
        return $this->res($res);
    }

    /**
     * Author:Yingbo Sun
     * 售后中心  0 未退款 1 申请中 2 已退款
     */
    public function afterSale()
    {
        $status = $this->request->get('status');
        $uid = $this->request->uid;
        if (empty($uid)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        $data = OrderService::orderAfterSale($status,$uid);
        return  $this->res($data);
    }

    /**
     * Author:William
     * 创建订单
     * 08/14  17：13
     */
    public function orderCreate(Request $request)
    {

        $order = $request->post();
        $order['user_id'] = $request->uid;
        try {
            validate(OrderCreateCheck::class)->check($order);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::fail($e->getError());
        }
        $data = OrderService::orderCreate($order);
//        return  $this->res(['code'=>200,'message'=>111,'data'=>$data]);
        return $this->res($data);
    }

    //订单支付  立即支付
    public function orderPay(Request $request)
    {
        //获取订单id
        //获取支付状态  1 立即付款  2未付款
        $post = $request->post();
        //验证
        $this->validateFail(OrderPayCheck::class, $post);
        $data = OrderService::orderPay($post['order_id'], $post['pay_id']);
        return $this->res($data);
    }

    /**
     * Author:William
     * @param int $goodsSkuId 商品skuId
     */
    public function orderPriceCheck(int $goodsSkuId)
    {
        //查询商品售价
        GoodsSku::where('id', $goodsSkuId)->field(['sku_price']);
    }

    public function orderConfirmReceive()
    {
        OrderService::orderConfirmReceive();
    }

    /**
     * Author:Yingbo Sun
     * 积分兑换商品
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderIntegralGoods()
    {
        $uid = $this->request->uid;
        $exchangeId = $this->request->post('id','0');
        $addressId = $this->request->post('aid','0');
        if (empty($exchangeId) || empty($addressId)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        $data = OrderService::orderIntegralGoods($exchangeId,$uid,$addressId);
        return  $this->res($data);
    }

}
